package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.SkuListResult.SkuInfo;
import com.da.lon.wang.xlg.view.SureSkuPopupWindow;
import com.osell.selection.widget.MyGridView;

public class SkuListAdapter extends BaseAdapter {
    SkuInfo datas;
    Context context;
    LayoutInflater inflater;

    public SkuListAdapter(SkuInfo datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null && datas.getArea_list() != null ? datas
                .getArea_list().size() + 1 : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.getArea_list().size() + 1 > position ? datas
                .getArea_list().get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (position) {
            case 0:
                convertView = getPartOne();
                break;
            default:
                convertView = getPartTwo(position);
                break;
        }
        return convertView;
    }

    /**
     * 第一行
     *
     * @return
     */
    private View getPartOne() {
        RelativeLayout convertView = null;
        try {
            final Holder holder = new Holder();

            if (convertView == null) {
                convertView = (RelativeLayout) inflater.inflate(
                        R.layout.sku_store_list_item, null);
            }
            holder.near_sku_rl = (RelativeLayout) convertView
                    .findViewById(R.id.near_sku_rl);
            holder.img = (ImageView) convertView
                    .findViewById(R.id.store_bigimage);
            holder.name = (TextView) convertView.findViewById(R.id.store_name);
            holder.dis = (TextView) convertView
                    .findViewById(R.id.store_distance);
            holder.name.setText(datas.getRecommend_info().getStore_name());
            String distance = datas.getRecommend_info().getKm() != null ? datas
                    .getRecommend_info().getKm() : "0";
            if ("0".equals(distance)) {
                holder.dis.setText("定位失败");
            } else if (Float.parseFloat(distance) > 1000) {
                int y = (Integer.parseInt(distance)) / 1000;
                holder.dis.setText("距离" + y + "千米");
            } else {
                holder.dis.setText("距离" + distance + "米");
            }

            MyApplication.loadImageByPicasso(context, datas.getRecommend_info()
                    .getImage_1(), null, holder.img, 0, 0, 1);

            holder.near_sku_rl.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    String state = datas.getRecommend_info().getState();
                    if (!state.isEmpty() && "1".equals(state)) {
                        SureSkuPopupWindow sureSkuPopupWindow = new SureSkuPopupWindow(
                                context, datas, 0, 0, 0);
                        // 显示窗口
                        sureSkuPopupWindow.showAtLocation(holder.near_sku_rl,
                                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
                                0);
                    }
                }
            });

            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;

    }

    /**
     * 第二行
     *
     * @param position
     * @return
     */
    private View getPartTwo(final int position) {

        LinearLayout convertView = null;
        try {
            final Holder holder = new Holder();

            if (convertView == null) {
                convertView = (LinearLayout) inflater.inflate(
                        R.layout.sku_item, null);
                holder.diqu_tv = (TextView) convertView
                        .findViewById(R.id.diqu_tv);
                holder.sku_scrollgridview = (MyGridView) convertView
                        .findViewById(R.id.sku_scrollgridview);
                holder.diqu_tv.setText(datas.getArea_list().get(position - 1)
                        .getName());
                holder.sku_scrollgridview.setAdapter(new SkuAdapter(context,
                        datas.getArea_list().get(position - 1).getStore_list(),
                        datas, position));
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    /**
     * 选择欧咖馆
     */
    private void chooesSKU() {

    }

    class Holder {
        public RelativeLayout near_sku_rl;
        public TextView dis;
        public MyGridView sku_scrollgridview;
        public TextView diqu_tv;
        ImageView img;
        TextView name;
    }

}
